from appium import webdriver
from appium.webdriver.common.appiumby import By
from time import sleep
from appium.webdriver.common.touch_action import TouchAction

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '7'
desired_caps['deviceName'] = '127.0.0.1:62001'
desired_caps['app'] = r'D:\apk\mymoney.apk'
desired_caps['appPackage'] = r'com.mymoney'
desired_caps['appActivity'] = r'com.mymoney.biz.splash.SplashScreenActivity'
desired_caps['noReset'] = False  # False代表重置（重新安装），默认值就是False

dr = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
dr.implicitly_wait(10)  # 隐式等待
sleep(3)

# 点击同意
dr.find_element(By.ID, 'com.mymoney:id/sui_dialog_positive_btn_style1').click()
sleep(1)
# 点击选账本
dr.find_element(By.ID, 'com.mymoney:id/select_template_btn').click()
sleep(1)
# 点跳过
dr.find_element(By.ID, 'com.mymoney:id/bottom_skip_tv').click()
sleep(3)

# dr.find_element(By.XPATH,
#                 '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout[2]/androidx.viewpager.widget.ViewPager/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.widget.LinearLayout/android.view.ViewGroup/android.widget.ScrollView/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView[1]/android.widget.FrameLayout/android.view.ViewGroup').click()
# 点击我的
dr.find_element(By.ID, 'com.mymoney:id/account_centre_icon_default_iv').click()
sleep(1)

y2 = dr.get_window_size()['height'] * 0.3
x1 = dr.get_window_size()['width'] * 0.5
y1 = dr.get_window_size()['height'] * 0.7
dr.swipe(x1, y1, x1, y2)


sleep(1)
# 点击设置
dr.find_element(By.XPATH,
                '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/androidx.viewpager.widget.ViewPager/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.ScrollView/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.widget.FrameLayout[9]/android.widget.LinearLayout/android.widget.TextView').click()
sleep(1)

dr.find_element(By.XPATH,
                '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/androidx.recyclerview.widget.RecyclerView/android.view.ViewGroup[1]').click()
sleep(1)
# 密码保护按钮
dr.find_element(By.ID, 'com.mymoney:id/right_switch').click()
sleep(1)
# 手势密码
dr.find_element(By.XPATH,
                '/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.LinearLayout/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]/android.widget.TextView').click()
sleep(1)

# 连续滑动设置第一次密码
for i in range(2):
    TouchAction(dr).press(x=251, y=381).wait(1000).move_to(x=448, y=381).wait(1000).move_to(x=645, y=381).wait(
        1000).move_to(x=645, y=576).wait(1000).release().perform()
